package cn.nfwx.tv.base;

import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.os.Looper;
import android.os.MessageQueue;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.squareup.leakcanary.RefWatcher;
import com.trello.rxlifecycle2.components.support.RxFragment;

import cn.nfwx.tv.BuildConfig;
import cn.nfwx.tv.widget.LoadView;

/**
 * Created by zb.liu on 2017/10/30.
 */

public abstract class BaseFragment extends RxFragment {
    public Context mContext;
    protected LoadView loadView;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getActivity();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return initView();
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (isLoadView()) {
            loadView = new LoadView(mContext);
        }
        bindView();
        initLister();
        initData();
    }

    protected void initLister() {
    }

    protected void bindView() {

    }

    /**
     * 有子类实现，实现特有效果
     *
     * @return
     */
    public abstract View initView();


    /**
     * 初始化数据
     */
    public void initData() {

    }

    /**
     * 是否需要loadview
     *
     * @return
     */
    protected boolean isLoadView() {
        return false;
    }

    /**
     * 查找控件
     *
     * @param rid
     * @param <T>
     * @return
     */
    public <T extends View> T findView(int rid) {
        return (T) getView().findViewById(rid);
    }

    public <T extends View> T findView(View view, int rid) {
        return (T) view.findViewById(rid);
    }
}